home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2001 / MacHack 2001.toast / pc / The Hacks / SpellCompositor / SpellBook.java < prev    next >
Encoding:
Java Source  |  2001-06-23  |  6.7 KB  |  263 lines

  1. /*
  2.     SpellBook
  3.         - Data-structure for the collection of SpellRecords and SpellRegisters
  4.     
  5.     Written by Scott C. Ziegler for use with Simulcra RPG and AD&D.
  6.     
  7.     
  8.     
  9.     vers. history:
  10.         09.07.00 - achieved a syntactically correct version
  11.     
  12.     Work Needed:
  13.          
  14.     
  15.     Work Completed:
  16.         - added an add and remove function
  17.         
  18.  
  19.  
  20. */
  21.  
  22. /*
  23. This file and its intellectual contents are considered property of the creator and are to be treated as such with respect to use in other applications.  Any use of this software for any purpose whatsoever must have explicit permission from the author of the file.  This code is part of an ongoing development process for future possible products, and so is considered private property.
  24. Do not use without permission.  Author: Scott C. Ziegler <Aslan@Narnia.net>
  25. */
  26.  
  27. package Arcana;
  28.  
  29. import java.awt.*;
  30. import javax.swing.*;
  31.  
  32. import java.io.*;
  33. import java.util.*;
  34.  
  35. public class SpellBook implements java.io.Serializable {
  36.     
  37.     // Data Members
  38.     
  39.     String        Name;
  40.     String        Creator;
  41.     String        Owner;
  42.     
  43.     Vector[]                SpellRecordArray = new Vector[9];
  44.     SpellRegisterVector[]    SpellRegisterArray = new SpellRegisterVector[9];
  45.     
  46.     int            noOfSpellsInBook;
  47.     int            maxNoOfSpells;
  48.     
  49.     
  50.     // Methods
  51.     
  52.     // Constructors
  53.     
  54.     public SpellBook() {
  55.         Name = "";
  56.         Creator = "";
  57.         Owner = "";
  58.         
  59.         //System.out.println("before init loop");
  60.         for (int i=0; i < 9; i++) {    
  61.             SpellRecordArray[i] = new Vector(50);
  62.             SpellRegisterArray[i] = new SpellRegisterVector();
  63.             }
  64.         //System.out.println("after init loop");
  65.         
  66.         noOfSpellsInBook = 0;
  67.         maxNoOfSpells = 1024;
  68.         }
  69.     
  70.     public SpellBook(SpellBook spellBook) {
  71.         Name = spellBook.getName();
  72.         Creator = spellBook.getCreator();
  73.         Owner = spellBook.getOwner();
  74.         
  75.         for (int i=0; i < 9; i++) {    
  76.             SpellRecordArray[i] = spellBook.getSpellRecordVectorByIndex(i);
  77.             SpellRegisterArray[i] = spellBook.getSpellRegisterVectorByIndex(i);
  78.             }
  79.         
  80.         noOfSpellsInBook = spellBook.getNoOfSpellsInBook();
  81.         maxNoOfSpells = spellBook.getMaxNoOfSpells();
  82.         }
  83.         
  84.     // Mutators
  85.         
  86.     public void setName(String name) {
  87.         Name = name;
  88.         }
  89.     
  90.     public void setCreator(String create) {
  91.         Creator = create;
  92.         }
  93.     
  94.     public void setOwner(String owner) {
  95.         Owner = owner;
  96.         }
  97.     
  98.     // WARNING: By level! (not index)
  99.     // not used much?
  100.     public void setSpellRecordVector(Vector newRecordVec, int level) {
  101.         SpellRecordArray[level - 1] = newRecordVec;
  102.         }
  103.         
  104.     public void setSpellRecordVectorByIndex(Vector newRecordVec, int index) {
  105.         SpellRecordArray[index] = newRecordVec;
  106.         }
  107.     
  108.     public void setSpellRecordArray(Vector[] spellVecArray) {
  109.         for (int i=0; i < 9; i++) {
  110.             SpellRecordArray[i] = spellVecArray[i];
  111.             }
  112.         }
  113.         
  114.     public void setSpellRegisterVector(SpellRegisterVector newRegisterVec, int level) {
  115.         SpellRegisterArray[level - 1] = newRegisterVec;
  116.         }
  117.         
  118.     public void setSpellRegisterVectorByIndex(SpellRegisterVector newRegisterVec, int index) {
  119.         SpellRegisterArray[index] = newRegisterVec;
  120.         }
  121.     
  122.     public void setSpellRegisterArray(SpellRegisterVector[] spellVecArray) {    
  123.         for (int i=0; i < 9; i++) {
  124.             SpellRegisterArray[i] = spellVecArray[i];
  125.             }
  126.         }
  127.     
  128.     protected void setNoOfSpellsInBook(int noInBook) {
  129.         noOfSpellsInBook = noInBook;
  130.         }
  131.     
  132.     public void setMaxNoOfSpells(int maxNoSpells) {
  133.         maxNoOfSpells = maxNoSpells;
  134.         }
  135.         
  136.     // Selectors
  137.     
  138.     public String getName() {    
  139.         return Name;
  140.         }
  141.     
  142.     public String getCreator() {
  143.         return Creator;
  144.         }
  145.     
  146.     public String getOwner() {
  147.         return Owner;
  148.         }
  149.     
  150.     // WARNING: this is by Level
  151.     public Vector getSpellRecordVector(int level) {
  152.         return SpellRecordArray[level - 1];
  153.         }
  154.     
  155.     public Vector getSpellRecordVectorByIndex(int index) {
  156.         return SpellRecordArray[index];
  157.         }
  158.         
  159.     public SpellRegisterVector getSpellRegisterVector(int level) {
  160.         return SpellRegisterArray[level - 1];
  161.         }
  162.         
  163.     public SpellRegisterVector getSpellRegisterVectorByIndex(int index) {
  164.         return SpellRegisterArray[index];
  165.         }
  166.         
  167.     public Vector[] getSpellRecordArray() {
  168.         return SpellRecordArray;
  169.         }
  170.     
  171.     public SpellRegisterVector[] getSpellRegisterArray() {    
  172.         return SpellRegisterArray;
  173.         }
  174.     
  175.     public int getNoOfSpellsInBook() {
  176.         return noOfSpellsInBook;
  177.         }
  178.     
  179.     public int getMaxNoOfSpells() {
  180.         return maxNoOfSpells;
  181.         }
  182.     
  183.     // Methods of importantcy
  184.     
  185.     // might need to rework the SpellRegister creation
  186.     public void addSpellRecord(SpellRecord newSpell) {
  187.         if ((noOfSpellsInBook + 1) < maxNoOfSpells) {
  188.             String spName = newSpell.getName();
  189.             // determine level of spell
  190.             int levelOfSpell = newSpell.getLevel();
  191.             // if spell of same name is not in book...
  192.             if (findSpell(spName, levelOfSpell).getName().compareTo("Untitled") == 0) {
  193.                 // get that level's vector for addition to
  194.                 Vector spellVector = getSpellRecordVector(levelOfSpell);
  195.                 spellVector.addElement(newSpell);
  196.                 // get that level's register vector for addition to
  197.                 SpellRegisterVector spellRegVec = getSpellRegisterVector(levelOfSpell);
  198.                 spellRegVec.addSpell(newSpell);
  199.                 noOfSpellsInBook++;
  200.                 }
  201.             else {
  202.                 JOptionPane.showMessageDialog(null, "That spell already exists!", 
  203.                                         "Error: Spell w/ Same Name", JOptionPane.ERROR_MESSAGE);
  204.                 }
  205.             }
  206.         else {
  207.             JOptionPane.showMessageDialog(null, "Maximum number of spells already in book!", 
  208.                                         "Error: Max No. Spells Reached", JOptionPane.ERROR_MESSAGE);
  209.             }
  210.         }
  211.     
  212.     // might need to rework the SpellRegister creation
  213.     public void removeSpellRecord(SpellRecord rmSpell) {
  214.         // determine level of spell
  215.         int levelOfSpell = rmSpell.getLevel();
  216.         // traverse Enumeration finding spell
  217.         // delete that element from vector (might do above)
  218.         Vector spellVector = getSpellRecordVector(levelOfSpell);
  219.         if (!spellVector.removeElement(rmSpell)) {
  220.             // JOptionPane: it didn't work!
  221.             JOptionPane.showMessageDialog(null, "The spell was not deleted!", 
  222.                                         "Error: Spell Not Deleted", JOptionPane.ERROR_MESSAGE);
  223.             }
  224.         // deletion from register
  225.         SpellRegisterVector spellRegVec = getSpellRegisterVector(levelOfSpell);
  226.         spellRegVec.removeSpell(rmSpell);
  227.         // decrement counter
  228.         noOfSpellsInBook--;
  229.         }
  230.     
  231.     public SpellRecord findSpellByName(String name) {
  232.         Enumeration e;
  233.         for (int i=0; i < 9; i++) {
  234.             e = getSpellRecordVectorByIndex(i).elements();
  235.             SpellRecord spellRec = new SpellRecord();
  236.             while (e.hasMoreElements()) {
  237.                 spellRec = (SpellRecord)e.nextElement();
  238.                 if (name.compareTo(spellRec.getName()) == 0) {
  239.                     return spellRec;
  240.                     }
  241.                 }
  242.             }
  243.         // this point in the code i reached only if the rec is not found
  244.         return new SpellRecord();
  245.         }
  246.     
  247.     public SpellRecord findSpell(String name, int level) {
  248.         Enumeration e = getSpellRecordVectorByIndex(level-1).elements();
  249.         SpellRecord spellRec = new SpellRecord();
  250.         while(e.hasMoreElements()) {
  251.             spellRec = (SpellRecord)e.nextElement();
  252.             if (name.compareTo(spellRec.getName()) == 0) {
  253.                 return spellRec;
  254.                 }
  255.             }
  256.         // this point in the code i reached only if the rec is not found
  257.         return new SpellRecord();
  258.         }
  259.     
  260.     }
  261.     
  262.     
  263.